package com.example.cdpbookstore.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.example.cdpbookstore.dao.CustomerDao;
import com.example.cdpbookstore.dao.impl.CustomerDaoImpl;
import com.example.cdpbookstore.pojo.entity.Customer;
import com.example.cdpbookstore.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {
    private final CustomerDao customerDao = new CustomerDaoImpl();
    @Override
    public boolean save(Customer customer) {
        return customerDao.save(customer);
    }

    @Override
    public Customer getByUsernameOrEmail(String account) {
        return customerDao.getByUsernameOrEmail(account);
    }

    @Override
    public Customer login(String account, String password) {
        Customer customer = getByUsernameOrEmail(account);
        if (customer != null) {
            // 比对密码
            if (BCrypt.checkpw(password, customer.getPassword())) {
                return customer;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public Customer getById(String id) {
        return customerDao.getById(id);
    }

    @Override
    public boolean update(Customer customer) {
        return customerDao.update(customer);
    }
}
